package models;

import java.util.Random;

public class Croupier {

    public static int whichPlayerNext2(int actualPlayer, Player players[],
	    boolean allinAllowed) throws GameException {
	int next = whichPlayerNext(actualPlayer, players.length - 1);
	if (allinAllowed) {
	    while (players[next].getStatus() <= Player.PLAYER_OUT_OF_GAME)
		next = whichPlayerNext(next, players.length - 1);
	} else {
	    while (players[next].getStatus() != Player.PLAYER_IN_GAME)
		next = whichPlayerNext(next, players.length - 1);
	    if (actualPlayer == next)
		throw new GameException("Only one player in game");
	}
	return next;
    }

    public static int whichPlayerNext(int actualPlayer, int playersAmount) {
	int next = actualPlayer - 1;
	next = (next + 1) % playersAmount;
	return (next + 1);
    }

    public static int drawDealer(int playersAmount) {
	Random random = new Random();
	return (random.nextInt(playersAmount) + 1);
    }

    /*
     * Method determines which buttons can be pressed based on players coins,
     * bet, maxBet and round
     * H-Check,B-Bet,R-Raise,C-Call,F-Fold,A-Allin,E-Exchange
     */
    public static String possibleMoves(int bet, int coins, boolean isBet,
	    int maxBet, int round) {
	String result = null;
	if (round == 1) {
	    if (bet < maxBet) {
		if (coins == (maxBet - bet))
		    result = "CF";
		else if (coins > (maxBet - bet))
		    result = "CRF";
		else if (coins < (maxBet - bet))
		    result = "FA";
	    } else if (bet == maxBet) {
		if (coins > 0)
		    result = "RF";
		else
		    result = "FA";
	    }
	} else {
	    if (!isBet) {
		if (coins > 0)
		    result = "HBF";
		else
		    result = "HF";// czy powinno być allin?
	    } else {
		if (bet < maxBet) {
		    if (coins == (maxBet - bet))
			result = "CF";
		    else if (coins > (maxBet - bet))
			result = "CRF";
		    else if (coins < (maxBet - bet))
			result = "FA";
		} else if (bet == maxBet) {
		    if (coins > 0)
			result = "RF";
		    else
			result = "FA";
		}
	    }
	}
	return result;
    }

    public static boolean havePlayersTheSameBets(Player players[]) {
	int i = 1;
	while (players[i].getStatus() != Player.PLAYER_IN_GAME) {
	    i++;
	}
	if (i == players.length) {
	    return true;
	} else {
	    int bet = players[i].getBet();
	    for (int j = 1; j < players.length; j++) {
		if (players[j].getStatus() == Player.PLAYER_IN_GAME
			&& players[j].getBet() != bet) {
		    return false;
		}
	    }
	    return true;
	}
    }

    public static boolean havePlayersTheSameBets(int playersStatuses[],
	    int playersBets[]) {
	int i = 1;
	while (playersStatuses[i] != Player.PLAYER_IN_GAME) {
	    i++;
	}
	if (i == playersStatuses.length) {
	    return true;
	} else {
	    int bet = playersBets[i];
	    for (int j = 1; j < playersStatuses.length; j++) {
		if (playersStatuses[j] == Player.PLAYER_IN_GAME
			&& playersBets[j] != bet) {
		    return false;
		}
	    }
	    return true;
	}
    }
}
